// mypage/replacePhone/replacePhone.js
var app=getApp();
var util = require("../../utils/util");
var config=require("../../api/config");
var my=require("../../api/my");



Page({
  /*页面的初始数据*/
  data: {
    mobile:'',
    mob1: '',
    show_ver: false,
    focus: true,
    time: 60,
    length:6,
    isFocus:false,
    Value:"",
    show_btn: false,
    userinfo: '',
    login_info: ''
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    that.setData({userinfo:wx.getStorageSync('userinfo'),login_info:wx.getStorageSync('login_info')})
  },
  // 手机号
  // mobile_focus(e){
  //   var that = this;
  //   var val = e.detail.value;
  //   var mob1 = util.insertStr(util.insertStr(val,3," "),8," ");
  //   that.setData({mobile:val, mob1:mob1})
  // }, 
  mobile_tap(e){
    var that = this;
    that.setData({ mobile:e.detail.value })
  },
  send_mobile:function(){
    var that = this,mobile = that.data.mobile,userinfo=that.data.userinfo;
    var neg=(/^1[3456789]\d{9}$/);
    if(!neg.test(mobile)||!mobile){
      util.page_show_toast('手机号码输入有误',1500)
      return false;
    }
    if(mobile==userinfo.phone){
      util.page_show_toast('手机号码未更换',1500)
      return false;
    }
    //
    that.send_code(mobile)
  },
  // 验证码
  ver_focus(e){
    var that = this;
    var val = e.detail.value,userinfo=that.data.userinfo;
    that.setData({ Value:val })
    if(val.length == 6){
      that.change_phone(userinfo.id,that.data.mobile,val)
    }
  }, 
  ver_tap(){
    var that = this;
    that.setData({ isFocus:true, })
  },
  // 倒计时
  interval:function(){
    var that = this,time=that.data.time;
    var tms=setInterval(function(){
      time--;
      that.setData({time: time})
      if(time==0){
        clearInterval(tms)
        that.setData({time: 60,show_btn: true})
      }
    },1000)
  },
  // 重发验证码
  again_send:function(){
    var that = this,show_btn=that.data.show_btn;
    if(show_btn){
      //
      that.interval();
      that.setData({show_btn: false});
      //
      that.send_code(that.data.mobile);
    }
  },
  // 发送验证码请求
  send_code:function(mobile){
    var that=this;
    config.send_code({"phone":mobile}).then(res=>{
      // console.log(res)
      if(res.data.code==200){
        that.setData({show_ver: true,isFocus: true});
        that.interval();
        setTimeout(function(){
          util.page_show_toast('验证码已发送',1500)
        },500)
      }
    })
  },
  // // 验证手机验证码
  // very_code:function(mobile,code){
  //   var that=this;
  //   config.very_code({"mobile":mobile,"code":code}).then(res=>{
  //     if(res.data.code==200){
  //       that.change_phone(1,mobile,code)
  //     }
  //   })
  // },
  // 更换手机
  change_phone:function(id,mobile,code){
    var that=this,login_info=that.data.login_info;
    my.change_phone({"id":id,"phone":mobile,"code":code}).then(res=>{
      // console.log(res)
      if(res.data.code==200){
        app.globalData.phone=mobile;
        app.globalData.change_phone=true;
        util.page_show_toast('更换成功',1500)
        login_info.phone=mobile;
        wx.setStorageSync('login_info', login_info)
        setTimeout(()=>{
          wx.navigateBack({
            delta: 1
          });
        },1500)
      }else{
        util.page_show_toast(res.data.message,2000)
        setTimeout(()=>{
          wx.navigateBack({
            delta: 1
          });
        },2000)
      }
    })
  },
})